package binaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author kunkun
 * @className LeetCode_199
 * @Description ...
 * @date 2025/3/14 15:15
 */
public class LeetCode_199 {
    //节点结构
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> rightSideView(TreeNode root) {

        ArrayList<Integer> list = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.addFirst(root);
        queue.addFirst(null);
        while (!queue.isEmpty()){
            TreeNode node = queue.removeLast();
            if (node==null){
                break;
            }
            if (node.left!=null){
                queue.addFirst(node.left);
            }
            if (node.right!=null){
                queue.addFirst(node.right);
            }
            if (queue.getLast()==null){
                list.add(node.val);
                queue.removeLast();
                queue.addFirst(null);
            }
        }
        return list;


    }
}
